package cn.wl.hrm;

import cn.wl.hrm.doc.CourseDoc;
import cn.wl.hrm.repository.CourseElasticsearchRepository;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Optional;

@RunWith(SpringRunner.class)
@SpringBootTest(classes =  ESServerApplication2050.class)
public class ESTest {
    //创建工具
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    //导入依赖，用来做增删改
    @Autowired
    private CourseElasticsearchRepository courseElasticsearchRepository;

    @Test
    public void testCreateIndex(){
        //创建索引库
        elasticsearchTemplate.createIndex(CourseDoc.class);
        //做文档映射--相当于mysql建表
        elasticsearchTemplate.putMapping(CourseDoc.class);
    }

    /**
     *课程添加
     */
    @Test
    public void testAdd(){
        CourseDoc courseDoc = new CourseDoc();
        for (int i = 0; i < 50; i++) {
            courseDoc.setId(i+2L);
            if(i%2 == 0){
                courseDoc.setName("java基础");
                courseDoc.setDescription("java简单易学");
                courseDoc.setPrice(188F);
                courseDoc.setCourseTypeId(111L);
            }else{
                courseDoc.setName("H5前端");
                courseDoc.setDescription("H5简单易学");
                courseDoc.setPrice(166F);
                courseDoc.setCourseTypeId(222L);
            }
            courseElasticsearchRepository.save(courseDoc);
        }
        System.out.println("保存成功");
    }

    /**
     *课程搜索：
     * 条件：
     *课程的名字name包含java
     *课程类型是111
     * 价格150-500
     *根据价格倒序排列
     * 分页查询，每页10条
     */
    @Test
    public void testSearch(){
        //构建查询
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        //添加查询条件
        //创建组合查询对象
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.matchQuery("name","java"));
        //过滤条件查询
        List<QueryBuilder> filter = boolQueryBuilder.filter();
        //使用term等值查询：课程类型为111的
        filter.add(QueryBuilders.termQuery("courseTypeId",111L));
        //使用range范围查询
        filter.add(QueryBuilders.rangeQuery("price").gte(150).lte(500));
        nativeSearchQueryBuilder.withQuery(boolQueryBuilder);
        //排序
        nativeSearchQueryBuilder.withSort(new FieldSortBuilder("price").order(SortOrder.DESC));
        //分页
        nativeSearchQueryBuilder.withPageable(PageRequest.of(0,10));

        //构建查询对象
        SearchQuery searchQuery = nativeSearchQueryBuilder.build();
        //执行查询方法
        Page<CourseDoc> page = courseElasticsearchRepository.search(searchQuery);
        //获取页数
        int totalPages = page.getTotalPages();
        System.out.println("页数："+totalPages);
        //获取数据条数
        long totalElements = page.getTotalElements();
        System.out.println("数据条数："+totalElements);
        //获取查询出来的数据
        List<CourseDoc> courseDocs = page.getContent();
        //lamda表达式遍历
        courseDocs.forEach(c->{
            //c为courseDocs中每一条数据
            System.out.println(c);
        });



    }
}
